<?php


namespace app\common\model\mysql;


class Category extends BaseModel
{
    public function getNormalCategorys($field = "*"){
        $where = [
            'status' => config("status.mysql.table_normal"),
        ];
        $order = [
            "listorder" => "desc",
            "id" => 'desc',
        ];
        $result = $this->where($where)
            ->field($field)
            ->order($order)
            ->select();
        return $result;
    }

    public function getLists($data, $num = 10){
        $order = [
            "listorder" => "desc",
            "id" => 'desc',
        ];
        $result = $this->where("status", "<>", config("status.mysql.table_delete"))
            ->where($data)
            ->order($order)
            ->paginate($num);
        // echo $this->getLastSql();exit;
        return $result;
    }

    public function getById($id){
        $result = $this->find($id);
        if(empty($result)){
            return [];
        }
        $result = $result->toArray();
        return $result;
    }

    public function getChildCountInPids($condition){
        $where[] = ["pid", "in", $condition['pid']];
        $where[] = ["status", "<>", config('status.mysql.table_delete')];
        $res = $this->where($where)
            ->field(["pid", "count(*) as count"])
            ->group("pid")
            ->select();
        //dump($this->getLastSql());die;
        return $res;
    }

    public function getNromalByPid($pid, $field){
        $where = [
            "pid" => $pid,
            "status" => config("status.mysql.table_normal"),
        ];
        $order = [
            "listorder" => "desc",
            "id" => "desc"
        ];
        $res = $this->where($where)
            ->field($field)
            ->order($order)
            ->select();
        return $res;
    }
}